package com.agh.portal.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findGroupEventsByAghGroup", "findGroupEventsByStudent" })
public class GroupEvent {

    @Size(max = 80)
    @NotEmpty
    private String title;

    @Size(max = 1500)
    @NotEmpty
    private String content;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date eventDate;

    @ManyToOne
    private AghGroup aghGroup;

    @ManyToOne
    @NotNull
    private Student student;

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
	
    public static List<GroupEvent> findAllGroupEvents() {
        return entityManager().createQuery("SELECT o FROM GroupEvent o ORDER BY o.eventDate desc", GroupEvent.class).getResultList();
    }
    
    public static List<GroupEvent> findGroupEventEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM GroupEvent o ORDER BY o.eventDate desc", GroupEvent.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }
}
